package de.androidDevelopers.communityTeacherApp.models;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import de.androidDevelopers.communityTeacherApp.models.PupilInfo;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table PUPIL_INFO.
*/
public class PupilInfoDao extends AbstractDao<PupilInfo, Long> {

    public static final String TABLENAME = "PUPIL_INFO";

    /**
     * Properties of entity PupilInfo.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Forname = new Property(1, String.class, "Forname", false, "FORNAME");
        public final static Property Surname = new Property(2, String.class, "Surname", false, "SURNAME");
        public final static Property Birthday = new Property(3, String.class, "Birthday", false, "BIRTHDAY");
        public final static Property Reference_Number = new Property(4, String.class, "Reference_Number", false, "REFERENCE__NUMBER");
        public final static Property PictureID = new Property(5, Integer.class, "PictureID", false, "PICTURE_ID");
        public final static Property ClassFK = new Property(6, Long.class, "ClassFK", false, "CLASS_FK");
        public final static Property ContactInfoFK = new Property(7, Long.class, "ContactInfoFK", false, "CONTACT_INFO_FK");
        public final static Property PupilInfoFK = new Property(8, Long.class, "PupilInfoFK", false, "PUPIL_INFO_FK");
    };

    private DaoSession daoSession;

    private Query<PupilInfo> subjectInfo_PupilInfoListQuery;

    public PupilInfoDao(DaoConfig config) {
        super(config);
    }
    
    public PupilInfoDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'PUPIL_INFO' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'FORNAME' TEXT," + // 1: Forname
                "'SURNAME' TEXT," + // 2: Surname
                "'BIRTHDAY' TEXT," + // 3: Birthday
                "'REFERENCE__NUMBER' TEXT," + // 4: Reference_Number
                "'PICTURE_ID' INTEGER," + // 5: PictureID
                "'CLASS_FK' INTEGER," + // 6: ClassFK
                "'CONTACT_INFO_FK' INTEGER," + // 7: ContactInfoFK
                "'PUPIL_INFO_FK' INTEGER);"); // 8: PupilInfoFK
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'PUPIL_INFO'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, PupilInfo entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String Forname = entity.getForname();
        if (Forname != null) {
            stmt.bindString(2, Forname);
        }
 
        String Surname = entity.getSurname();
        if (Surname != null) {
            stmt.bindString(3, Surname);
        }
 
        String Birthday = entity.getBirthday();
        if (Birthday != null) {
            stmt.bindString(4, Birthday);
        }
 
        String Reference_Number = entity.getReference_Number();
        if (Reference_Number != null) {
            stmt.bindString(5, Reference_Number);
        }
 
        Integer PictureID = entity.getPictureID();
        if (PictureID != null) {
            stmt.bindLong(6, PictureID);
        }
 
        Long ClassFK = entity.getClassFK();
        if (ClassFK != null) {
            stmt.bindLong(7, ClassFK);
        }
 
        Long ContactInfoFK = entity.getContactInfoFK();
        if (ContactInfoFK != null) {
            stmt.bindLong(8, ContactInfoFK);
        }
    }

    @Override
    protected void attachEntity(PupilInfo entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public PupilInfo readEntity(Cursor cursor, int offset) {
        PupilInfo entity = new PupilInfo( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // Forname
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // Surname
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // Birthday
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // Reference_Number
            cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5), // PictureID
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // ClassFK
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7) // ContactInfoFK
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, PupilInfo entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setForname(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setSurname(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setBirthday(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setReference_Number(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setPictureID(cursor.isNull(offset + 5) ? null : cursor.getInt(offset + 5));
        entity.setClassFK(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setContactInfoFK(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(PupilInfo entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(PupilInfo entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "pupilInfoList" to-many relationship of SubjectInfo. */
    public List<PupilInfo> _querySubjectInfo_PupilInfoList(Long PupilInfoFK) {
        synchronized (this) {
            if (subjectInfo_PupilInfoListQuery == null) {
                QueryBuilder<PupilInfo> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.PupilInfoFK.eq(null));
                subjectInfo_PupilInfoListQuery = queryBuilder.build();
            }
        }
        Query<PupilInfo> query = subjectInfo_PupilInfoListQuery.forCurrentThread();
        query.setParameter(0, PupilInfoFK);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getClassInfoDao().getAllColumns());
            builder.append(" FROM PUPIL_INFO T");
            builder.append(" LEFT JOIN CLASS_INFO T0 ON T.'CLASS_FK'=T0.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected PupilInfo loadCurrentDeep(Cursor cursor, boolean lock) {
        PupilInfo entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ClassInfo classInfo = loadCurrentOther(daoSession.getClassInfoDao(), cursor, offset);
        entity.setClassInfo(classInfo);

        return entity;    
    }

    public PupilInfo loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<PupilInfo> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<PupilInfo> list = new ArrayList<PupilInfo>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<PupilInfo> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<PupilInfo> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
